package cn.sdormitory.job.evaluation;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.sdormitory.attendance.dao.HolidayConfigDao;
import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.evaluation.dao.WorkDayDao;
import cn.sdormitory.evaluation.dao.WorkWeekDao;
import cn.sdormitory.evaluation.entity.WorkWeekEntity;
import cn.sdormitory.evaluation.vo.WorkDayVO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
public class WorkWeekJob extends QuartzJobBean {
    @Resource
    HolidayConfigDao holidayConfigDao;
    @Resource
    WorkWeekDao workWeekDao;
    @Resource
    WorkDayDao workDayDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //每周一生成周班会任务
        int i = DateUtil.dayOfWeek(new Date());
        //1-表示周日 2-表示周一
        if(i==6){//周一生成任务
            String today = DateUtil.formatDate(DateUtil.beginOfWeek(new Date()));
            DateTime endTime = DateUtil.endOfWeek(new Date());
            //通过学生表去查询到有多少班级，班级信息，班主任信息
            List<WorkDayVO> workDayVOs = workDayDao.selectClassInfo();
            for (WorkDayVO workDayVO : workDayVOs){
                WorkWeekEntity entity = new WorkWeekEntity();
                entity.setClassId(workDayVO.getId());
                entity.setClassName(workDayVO.getClassName());
                entity.setStudentNumber(workDayVO.getStudentNumber());
                entity.setBeginDate(today);
                entity.setEndDate(DateUtil.formatDate(endTime));
                entity.setRegistrant(workDayVO.getRegistrant());
                entity.setCreateBy("SYSTEM");
                entity.setCreatedTime(new Date());
                workWeekDao.insert(entity);
            }
        }
    }

    public static void main(String[] args) {
        int i = DateUtil.dayOfWeek(new Date());
        System.out.println(DateUtil.formatDate(DateUtil.endOfWeek(new Date())));
    }
}
